<?php

namespace app\index\controller;

use app\BaseController;
use app\index\model\Feedback as ModelFeedback;
use frame\lib\Db;

class Feedback extends Base
{
    public function index()
    {
        $this->view();
    }
    public function add()
    {
        $switch = config('system', 'switch')['feedback'];
        if ($switch == 0) error('当前反馈功能已关闭，请稍后尝试 !');
        $content = @$_POST['content'];
        $uid = $this->uid;
        if (!empty($content)) {
            $data = [
                'content' => preventXSS($content),
                'uid' => 0,
                'user_id' => $uid,
                'create_time' => getTime()
            ];
            request_limit('feedback', "user_id = $uid", 30);
            $res = Db::name('feedback')->insert($data);
            if ($res) {
                success();
            }
            error();
        }
    }
    public function list()
    {
        $query = @$_GET['feedback'];
        $list = ModelFeedback::list($query);
        success($list);
    }
    public function del()
    {
        $id = @$_GET['id'];
        if (!$id) error('缺少参数ID', 201);
        $id = explode(',', $id);
        $res = Db::name('feedback')->where(['user_id'  => $this->uid, 'delete_time' => 0])->whereIn('id', $id)->delete();
        if ($res) success();
        error();
    }
    public function delAll()
    {
        $res = Db::name('feedback')->where(['user_id' => $this->uid])->delete();
        if ($res) success();
        error();
    }
}
